<template>
  <div class='login_container'>
    <div class='login_box'>
      <div class='logo_box'>
        <img src='../assets/logo.png' alt />
      </div>
      <!-- 登录区域 -->
      <div>
        <el-form label-width='0px' class='myform' :model='loginForm' :rules='rules' ref='formRef'>
          <!-- 用户名 -->
          <el-form-item prop='username'>
            <el-input prefix-icon='iconfont icon-user' v-model='loginForm.username'></el-input>
          </el-form-item>
          <!-- 密码区 -->
          <el-form-item prop='password'>
            <el-input
              prefix-icon='iconfont icon-3702mima'
              type='password'
              v-model='loginForm.password'
            ></el-input>
          </el-form-item>
          <!-- 按钮区 -->
          <el-form-item class='btn'>
            <el-button type='primary' @click='login'>登录</el-button>
            <el-button type='info' @click='reset'>重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reset () {
      this.$refs.formRef.resetFields()
    },
    login () {
      this.$refs.formRef.validate(async res => {
        if (res) {
          const { data } = await this.$http.post('login', this.loginForm)
          if (data.meta.status !== 200) {
            this.$message.error('登录失败')
          } else {
            const that = this
            this.$message.success({
              message: data.meta.msg,
              duration: 1000,
              onClose () {
                sessionStorage.setItem('login_token', data.data.token)
                that.$router.push('/home')
              }
            })
          }
        }
      })
    }
  }
}
</script>

<style lang='less' scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  border-radius: 5px;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  box-shadow: 2px 5px 2px grey;

  .logo_box {
    position: relative;
    width: 130px;
    height: 130px;
    left: 50%;
    transform: translate(-50%, -50%);
    border: 1px solid rgba(91, 133, 22, 0.418);
    border-radius: 50%;
    background-color: rgba(255, 255, 255, 0.198);
    box-shadow: 0, 2px, 2px, rgba(115, 111, 111, 0.486);
    img {
      width: 100%;
      height: 100%;
    }
  }
}
.myform {
  position: absolute;
  width: 100%;
  padding: 20px;
  box-sizing: border-box;
  top: 30%;
  .btn {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
